import json

from luckin.models import ShippingAddress
from luckin.common import valid_request_param
from luckin.form import ShippingAddressForm


class ShippingAddressService:

    # 获取收获地址信息
    def get(self, request):
        if not valid_request_param(request):
            return False, "参数错误"

        # 虽说没啥必要，但是还是校验下，_q必须为空字典
        _q = request.query_params.get("_q")
        param_dict = json.loads(_q)
        if param_dict:
            return False, "参数错误"

        user_id = request.user.id
        address_query_set = ShippingAddress.objects.filter(user_id=user_id)
        return True, address_query_set or []

    def post(self, request):
        # _q:{"nickname":"%s","gender":"%s","phone":"%s","address":"%s","home_number":"%s","address_tag":"%s","is_default","1"}
        if not valid_request_param(request):
            return False, "参数错误"

        _q = request.data.get("_q")
        _s = request.data.get("_s")

        param_dict = json.loads(_q)
        user = request.user
        forms = ShippingAddressForm(param_dict)
        if forms.is_valid():
            # 默认地址只能一个，先判断是否为默认
            is_default = forms.cleaned_data.get("is_default")
            if int(is_default) == 1:
                ShippingAddress.objects.filter(user=user).update(is_default=2)
            address_obj = ShippingAddress.objects.create(user=user, **forms.cleaned_data)
            return True, address_obj
        else:
            error_key = list(forms.errors)[0]
            error_msg = str(forms.errors.get(error_key))
            return False, error_msg

    def put(self, request):
        # _q:{"id":"%s","nickname":"%s","gender":"%s","phone":"%s","address":"%s","home_number":"%s","address_tag":"%s"，"is_default","1"}
        if not valid_request_param(request):
            return False, "参数错误"

        _q = request.data.get("_q")
        _s = request.data.get("_s")

        param_dict = json.loads(_q)
        user = request.user
        forms = ShippingAddressForm(param_dict)
        if forms.is_valid():
            # 默认地址只能一个，先判断是否为默认
            is_default = forms.cleaned_data.get("is_default")
            address_id = param_dict.get("id")
            if not address_id:
                return False, "参数错误"
            if int(is_default) == 1:
                ShippingAddress.objects.filter(user=user).update(is_default=2)
            ShippingAddress.objects.filter(pk=address_id, user=user).update(**forms.cleaned_data)
            return True, "更新成功"
        else:
            error_key = list(forms.errors)[0]
            error_msg = str(forms.errors.get(error_key))
            return False, error_msg

    def delete(self, request):
        # _q : {"id":"%s"}
        if not valid_request_param(request):
            return False, "参数错误"

        _q = request.data.get("_q")
        _s = request.data.get("_s")

        # _q中的参数
        param_dict = json.loads(_q)
        address_id = param_dict.get("id")
        if not address_id or not address_id.isdigit():
            return False, "参数错误"

        user = request.user
        # 执行删除操作
        ShippingAddress.objects.filter(pk=address_id, user=user).delete()

        return True, "删除成功"
